package com.zxy.supplier_system.server.utils

import java.time.LocalDate
import java.util.*


class OrderNumberGenerator(
    initialCurrentSequence: () -> Int = { 0 },
    private val sequenceLength: Int = 2
) {

    var latestGeneratedDate: LocalDate = LocalDate.now()

    private var sequence: Int = initialCurrentSequence()

    fun generate(): String {
        // 获取当前日期
        val currentDate = LocalDate.now()
        if (this.latestGeneratedDate != currentDate)
            this.sequence = 0

        this.latestGeneratedDate = currentDate
        // 获取年份的后两位
        val year = currentDate.year.toString().substring(2)
        // 将日期转换为36进制的字符串
        val month = (currentDate.monthValue).toString(32)
        val day = (currentDate.dayOfMonth).toString(32)
        return "$year$month$day${(++sequence).toString().padStart(sequenceLength, '0')}".uppercase(Locale.getDefault())
    }
}

